package
{	
	import com.greensock.plugins.BezierPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	//import com.hurlant.crypto.hash.SHA256;
	
	import com.pblabs.engine.PBE;
	import com.pblabs.engine.debug.Logger;
	import com.solidumgame.martillo.Match;
	import com.solidumgame.martillo.events.MatchGenericEvent;
	import com.solidumgame.martillo.interfaces.gameOver.GameOverUI;
	import com.solidumgame.martillo.interfaces.start.StartGameUI;
	import com.solidumgame.martillo.utils.MyResources;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	[SWF(width="640", height="480", frameRate="30")]
	public class Martillo extends Sprite	 {

		protected var match:Match; 
		public static var theStage:Stage;
		private var startUI:StartGameUI;
		private var gameOverUI:GameOverUI;
		
		public var session_start:String;
		public var finalScore:String = "200";
		
		
		public function Martillo():void	{
			//Activate Tween plugins
			TweenPlugin.activate([BezierPlugin]);
			
			PBE.startup(this);
			PBE.addResources(new MyResources());
			theStage = stage;
			startUI = new StartGameUI(stage);
			startUI.addEventListener("startGame", startGame);			
			addChild(startUI);
			
			//descomentar despues
			//var loader : URLLoader = new URLLoader();
			//loader.dataFormat = URLLoaderDataFormat.VARIABLES;
			//loader.addEventListener(Event.COMPLETE,completeHandler);

			function completeHandler(event:Event):void{
				//var session_id : String = event.target.data.session_id;
				var user_id : String = event.target.data.user_id;
				var user_name : String = event.target.data.name;
				session_start = event.target.data.session_start;
				var session_id : String = event.target.data.session_start;
				
				trace(user_name);
			}
			
			
			
			// ==== POST VARIABLES For example ... fname=Fred&lname=Bloggs
			//var variables : URLVariables = new URLVariables(); 
			//variables.test = "test";
			
			// ==== Set up the php page URL that does the AJAX stuff. It returns the results in another query string.
			var myRequest: URLRequest = new URLRequest(); 
			myRequest.url    = "http://facebookgameserver.appspot.com/login"; 
			//myRequest.url    = "http://www.posttestserver.com"; 
			//myRequest.method = URLRequestMethod.POST; 
			//myRequest.data   = variables;
			
			try 
			{
				//descomentar despues
				//loader.load(myRequest);    // This sends the data to the php page.
			} 
			catch(error:Error)
			{ 
				trace("Could not load the remote script page."); 
			}
			
			
		}
		
		protected function startGame(e:Event):void
		{								
			match = new Match();
			match.addEventListener(MatchGenericEvent.MATCH_END_EVENT, gameOver);
		}
		
		protected function gameOver(e:MatchGenericEvent):void
		{
			match.removeEventListener(MatchGenericEvent.MATCH_END_EVENT, gameOver);
			gameOverUI = new GameOverUI(stage);
			gameOverUI.addEventListener("startGame",startGame);
			addChild(gameOverUI);
		
			finalScore = (e.finalScore+7).toString(6);
			
			function completeHandler(event:Event):void{
				//var session_id : String = event.target.data.session_id;
				var msg : String = event.target.data.msg;
				trace(msg);
			}
			
			var loader : URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.VARIABLES;
			loader.addEventListener(Event.COMPLETE,completeHandler);
			
			// ==== POST VARIABLES For example ... fname=Fred&lname=Bloggs
			var variables : URLVariables = new URLVariables(); 
			variables.finalScore = finalScore;
			variables.session_start = session_start;
			variables.game_id = 2;
			
			
			// ==== Set up the php page URL that does the AJAX stuff. It returns the results in another query string.
			var myRequest: URLRequest = new URLRequest(); 
			myRequest.url    = "http://facebookgameserver.appspot.com/login"; 
			//myRequest.url    = "http://www.posttestserver.com"; 
			//myRequest.method = URLRequestMethod.POST; 
			myRequest.data   = variables;
			
			try 
			{
				loader.load(myRequest);    // This sends the data to the php page.
			} 
			catch(error:Error)
			{ 
				trace("Could not load the remote script page."); 
			}
			
		}

	}

}